package com.tgpms.web.element.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.web.element.service.SubCheckService;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Slf4j
public class SubCheckServiceImpl implements SubCheckService {
	
	@Resource
    private VformInterface vformInterface;

	//查询合同集合
	@Override
	public Result getPoList(Result res,String dataowner) {
		JSONObject json = new JSONObject();
		String code = "SCWZC038";
		json.put("[SQL1]", "");
		json.put("[SQL2]", "");
		json.put("[SQL3]", "");
		res = vformInterface.multiForMsQueryBySql(code, json.toJSONString(),"1", "1000", "Y", "Y", dataowner);
		return res;
	}

	//查询合同物资设备树形
	@Override
	public Result getPoSubTreeList(Result res, String poNo,String name, String dataowner) {
		List<JSONObject> list = new ArrayList<JSONObject>();
		JSONObject resjson = new JSONObject();
		resjson.put("id", poNo);
		resjson.put("name", name);
		resjson.put("type", "P0");
		JSONObject json = new JSONObject();
		json.put("[SQL1]", "");
		json.put("[SQL2]", "");
		json.put("[SQL3]", " and t.po_no='"+poNo+"'");
		res = vformInterface.multiForMsQueryBySql("SCWZC039", json.toJSONString(),"1", "1000", "Y", "Y", dataowner);
		log.info("SCWZC039查询的结果为："+res);
		if(res==null || res.getData().toString().equals("{\"message\":\"login\"}") ) {
			res.setSuccess(false);
			res.setMsg("查询出错");
			return res;
		}
		JSONObject rjson = JSONObject.parseObject(res.getData().toString());
		if(rjson.get("total").toString().equals("0")) {
			res.setSuccess(true);
			res.setData(resjson);
			return res;
		}
		List<Map<String,String>> list1 = JSONArray.parseObject(rjson.get("rows").toString(), List.class);
		List<JSONObject> boqList = new ArrayList<JSONObject>();
		for(Map<String,String> map1 : list1) {
			log.info("SCWZC039循环当前数据为："+map1);
			JSONObject json1 = new JSONObject();
			String id1 = map1.get("ID");
			json1.put("id", id1);
			json1.put("name", map1.get("LABEL"));
			json1.put("type", map1.get("TYPE"));
			json.put("[SQL1]", "");
			json.put("[SQL2]", "");
			json.put("[SQL3]", " and t.po_no='"+poNo+"' and t.pid='"+id1+"'");
			res = vformInterface.multiForMsQueryBySql("SCWZC040", json.toJSONString(),"1", "1000", "Y", "Y", dataowner);
			log.info("SCWZC040查询的结果为："+res);
			if(res==null || res.getData().toString().equals("{\"message\":\"login\"}") ) {
				res.setSuccess(false);
				res.setMsg("查询出错");
				return res;
			}
			rjson = JSONObject.parseObject(res.getData().toString());
			if(rjson.get("total").toString().equals("0")) {
				boqList.add(json1);
				continue;
			}
			List<JSONObject> subList = new ArrayList<JSONObject>();
			List<Map<String,String>> list2 = JSONArray.parseObject(rjson.get("rows").toString(), List.class);
			for(Map<String,String> map2 : list2) {
				log.info("SCWZC040循环当前数据为："+map2);
				JSONObject json2 = new JSONObject();
				String id2 = map2.get("ID");
				json2.put("id", id2);
				json2.put("name", map2.get("LABEL"));
				json2.put("type", map2.get("TYPE"));
				json.clear();
				json.put("[SQL1]", " and t.po_no='"+poNo+"' and t.pid='"+id2+"'");
				res = vformInterface.multiForMsQueryBySql("SCWZC041", json.toJSONString(),"1", "1000", "Y", "Y", dataowner);
				log.info("SCWZC041查询的结果为："+res);
				if(res==null || res.getData().toString().equals("{\"message\":\"login\"}") ) {
					res.setSuccess(false);
					res.setMsg("查询出错");
					return res;
				}
				rjson = JSONObject.parseObject(res.getData().toString());
				if(rjson.get("total").toString().equals("0")) {
					subList.add(json2);
					continue;
				}
				List<JSONObject> sheList = new ArrayList<JSONObject>();
				List<Map<String,String>> list3 = JSONArray.parseObject(rjson.get("rows").toString(), List.class);
				for(Map<String,String> map3 : list3) {
					log.info("SCWZC041循环当前数据为："+map3);
					JSONObject json3 = new JSONObject();
					String id3 = map3.get("ID");
					json3.put("id", id3);
					json3.put("name", map3.get("LABEL"));
					json3.put("type", map3.get("TYPE"));
					json3.put("suppliesCode", map2.get("CODE"));
					json3.put("equipmentCode", map3.get("CODE"));
					json3.put("poItem", map3.get("PO_ITEM"));
					json3.put("psi", map3.get("PSI"));
					json3.put("status", map3.get("STATUS"));
					json3.put("byuser", map3.get("BYUSER"));
					json3.put("ytime", map3.get("YTIME"));
					sheList.add(json3);
					log.info("sheList为："+sheList);
				}
				json2.put("children", sheList);
				subList.add(json2);
				log.info("subList为："+subList);
			}
			json1.put("children", subList);
			boqList.add(json1);
		}
		resjson.put("children", boqList);
		list.add(resjson);
		res.setData(list);
		return res;
	}

}
